<!--validtion form-->
<script>
	$(document).ready(function(){

		$("#addItemForm").validate({
			rules : {
				itemCode : { required : true},
				itemName : { required : true, minlength: 2},
				itemUnit : { required : true, min : 1},
				categoryId : { required : true, min: 1}
			},
			messages : {
				itemCode : { required : 'Please enter item code'},
				itemName : { required : 'Please enter item name', minlength:jQuery.format("At least {0} characters required!")},
				itemUnit : { required : 'Please select item unit', min : 'Please select item unit'},
				categoryId : { required : 'Please select category', min : 'Please select category'}
			}
		});
	});
</script>
<?php include './pages/stockMenu.php'; ?>
<div class="mainContent">
	<div class = "masterData">
		<h3>Available Items</h3>
		<?php
			include("./system/dbconnection.php");
			if($result =mysql_query("SELECT * FROM items, categories, units WHERE items.categoryId=categories.categoryId AND items.itemUnit=units.unitId")){
				if(mysql_num_rows($result)){
					echo "<table border='1' cellspacing='0' width='100%' class='masterTable'>";
					echo "<tr><th>Category</th><th>Item Code</th><th>Item Name</th><th>Unit</th></tr>";
					while($row = mysql_fetch_array($result)){
						echo "<tr>";
						echo "<td>" . $row['categoryName'] . "</td>";
						echo "<td>" . $row['itemCode'] . "</td>";
						echo "<td>" . $row['itemName'] . "</td>";
						echo "<td>" . $row['unitCode'] . "</td>";
						echo "</tr>";
					}
					echo "</table>";			
				}else{
					echo "No Data in Database";
				}	
			}
		?>	
	</div>
	<div class="mainForm">
		<form action="./system/action/addItems.php" method="post" name="addItem" id="addItemForm">	
			<table cellspacing="10px">
				<tr>
					<td colspan='2px'>
						<?php
						if(isset($_SESSION['error']) && $_SESSION['error'] != ""){ ?>
							<div class="error">
								<?php echo $_SESSION['error']; ?>
							</div>
						<?php } else if(isset($_SESSION['success']) && $_SESSION['success'] != ""){?>
							<div class="success">
								<?php echo $_SESSION['success']; ?>
							</div>
						<?php }
						?>					
					</td>
				</tr>
				<tr>
					<td align="left">Item Code<label class="mandatory">*</label></td>
					<td><input type="text" name="itemCode"/></td>
				</tr>
				<tr>
					<td align="left">Item Name<label class="mandatory">*</label></td>
					<td><input type="text" name="itemName" /></td>
				</tr>
				<tr>
					<td align="left">Item Description</td>
					<td><textarea name="itemDescription" rows="2" style="width:160px"></textarea></td>
				</tr>
				<tr>
					<td align="left">Item Unit<label class="mandatory">*</label></td>
					<td>
						<select name="itemUnit" id="itemUnit" style="width:160px">
							<option value="0">Select</option>
							<?php 
								include './system/dbconnection.php';
								if($sql = mysql_query("SELECT * FROM units")){
									while($row = mysql_fetch_assoc($sql)){
										echo "<option value='".$row['unitId']."'>".$row['unitCode']."</option>";
									}
								}
							?>
						</select>					
					</td>
				</tr>
				<tr>
					<td align="left">Category <label class="mandatory">*</label></td>
					<td>
						<select name="categoryId" id="categoryId" style="width:160px">
							<option value="0">Select</option>
							<?php 
								include './system/dbconnection.php';
								if($sql = mysql_query("SELECT * FROM categories")){
									while($row = mysql_fetch_assoc($sql)){
										echo "<option value='".$row['categoryId']."'>".$row['categoryName']."</option>";
									}
								}
							?>
						</select>
					</td>
				</tr>
				<tr >
					<td></td>
					<td align="left"><input type="submit" value="Submit"/></td>
				</tr>
			</table>	
		</form>	
	</div>
</div>